package com.ming.springcloud.consumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.ming.springcloud.consumer.service.StudentService;
import com.ming.springcloud.consumer.util.ResponseEntity;
import com.ming.springcloud.consumer.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student_hystric")
public class StudentHystricController {
    @Autowired
    private StudentService studentService;

    @RequestMapping(value = "userInfo", method = RequestMethod.GET)
    public String test() {
        return studentService.hello("ming");
    }

    @RequestMapping(value = "findAllStudents", method = RequestMethod.GET)
    public ResponseEntity findAllStudent1(){
        String userJSON= studentService.findAllStudents("ming");

        JSONObject object = (JSONObject) JSONObject.parse(userJSON);
        Integer result =Integer.parseInt(object.get("code").toString());
        String msg= object.get("code").toString();
        System.out.println("出问题了="+result);
        if (result==500){
            return ResponseEntity.error(msg);
        }
        return  ResponseEntity.success().add("pageInfo",userJSON);
    }

    @RequestMapping(value = "findStudentsById/{id}", method = RequestMethod.GET)
    public ResponseEntity findStudentsById(@PathVariable("id")Integer id){
        String userJSON= studentService.findStudentsById("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);

        //获取回传的josn数据
        String stuName=object.get("stuName").toString();
        String stuNum=object.get("stuNum").toString();
        String stuClass=object.get("stuClass").toString();
        String serviceName=object.get("serviceName").toString();
        String ids=object.get("id").toString();

        Student student=new Student();
        student.setId(Integer.parseInt(ids));
        student.setServiceName(serviceName);
        student.setStuClass(stuClass);
        student.setStuName(stuName);
        student.setStuNum(stuNum);

        return  ResponseEntity.success().add("user",student);
    }

    @RequestMapping(value = "delStudent/{id}", method = RequestMethod.GET)
    public ResponseEntity delStudent(@PathVariable("id")Integer id){
        String userJSON= studentService.delStudent("ming",id);
        JSONObject object = (JSONObject) JSONObject.parse(userJSON);
        Integer result =Integer.parseInt(object.get("code").toString());
        if(result>0){
            return ResponseEntity.success();
        }else{
            return ResponseEntity.error("删除失败");
        }
    }

    @PostMapping("/editStudent")
    public ResponseEntity addOrEditUser(Student student){
        if(student==null){
            return ResponseEntity.error("参数错误");
        }
        if(student.getId()==null){//新增
            String userJSON=studentService.addStudent("ming",student);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("新增失败");
            }
        }else{
            String userJSON=studentService.editStudent("ming",student);

            JSONObject object = (JSONObject) JSONObject.parse(userJSON);
            Integer result =Integer.parseInt(object.get("code").toString());

            if(result>0){
                return ResponseEntity.success();
            }else{
                return ResponseEntity.error("修改失败");
            }
        }
    }
}
